import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"
import { Api } from "/@/api/url"
import { getBizUrl } from '/@/utils/common/bizUrlUtils'

export const getExportUrl = Api.exportUserXls
export const getImportUrl = Api.importRoleExcel

export const listRole = (params) => {
  return defHttp.get({ url: "/iam-service/role/listRole", params })
}

export const pageRole = (params) => 
  defHttp.get({ url: "/iam-service/role/pageRole", params })

export const deleteRole = (params, handleSuccess) => {
  return defHttp.delete({ url: "/iam-service/role/delete", params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const deleteAdminRole = (params, handleSuccess) => {
  return defHttp.delete({ url: "/iam-service/system/role/delete", params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}


/**
 * 获取用户的角色列表
 */
export const listRoleByUserId = (params) => 
  defHttp.get({ url: "/system-service/role/listRoleByUserId", params })


export const batchDeleteRole = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: "/system-service/role/deleteBatch", data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const batchDeleteAdminRole = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: "/iam-service/system/role/deleteBatch", data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const saveOrUpdateRole = (params, isUpdate) => {
  const saveRole = "/iam-service/role/save"
  const updateRole = "/iam-service/role/update"
  const url = isUpdate ? updateRole : saveRole
  return defHttp.post({ url: url, params })
}

export const saveOrUpdateAdminRole = (params, isUpdate) => {
  const saveRole = "/iam-service/system/role/save"
  const updateRole = "/iam-service/system/role/update"
  const url = isUpdate ? updateRole : saveRole
  return defHttp.post({ url: url, params })
}


export const isRoleExist = (params) =>
  defHttp.get({ url: Api.isRoleExist, params }, { isTransformResponse: false })

export const listDeptTreeForRole = () => 
  defHttp.get({ url: Api.listDeptTreeForRole })

export const listDeptInRole = (params) => 
  defHttp.get({ url: Api.listDeptInRole, params })

export const saveRolePermission = (params) => 
  defHttp.post({ url: "/system-service/role/addRolePermission", params })

export const updateDataScope = (params) => 
  defHttp.post({ url: Api.updateRoleDataScope, params })

export const queryDataRule = (params) =>
  defHttp.get(
    {
      url: Api.queryRoleDataRule,
      params: {
        permissionId: params.menuId,
        roleId: params.roleId,
      },
    },
    { isTransformResponse: true }
  )

export const saveDataRule = (params) => 
  defHttp.post({ url: Api.saveRoleDataRule, params })

export const getParentDesignList = () => 
  defHttp.get({ url: Api.getParentDesignList })

export const getRoleDegisnList = (params) => 
  defHttp.get({ url: Api.getRoleDegisnList, params })
export const saveRoleDesign = (params) => 
  defHttp.post({ url: Api.saveRoleDesign, params })

export const pageSubject = (params) => 
  defHttp.get({ url: Api.listRoleUser, params })

export const pageAdminSubject = (params) => 
  defHttp.get({ url: "/iam-service/system/role/listSubject", params })


export const deleteAdminRoleSubject = (params, handleSuccess) => {
  return defHttp.delete({ url: "/iam-service/system/role/deleteSubject", params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const deletebRoleSubject = (params, handleSuccess) => {
  return defHttp.delete({ url: "/iam-service/role/deleteSubject", params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const batchDeleteAdminRoleSubject = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: "/iam-service/system/role/deleteSubject", params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const batchDeleteRoleSubject = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: "/iam-service/role/deleteSubject", params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}


export const addUserRole = (params, handleSuccess) => {
  return defHttp.post({ url: Api.addRoleUser, params }).then(() => {
    handleSuccess()
  })
}
